package com.wuliqing.android.zte.presenter;

import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;

/**
 * Created by 10172915 on 2015/7/7.
 */
public class LoadContactListPresenter {
    private Activity mActivity;
    private LoadContactDataListener mLoadContactDataListener;
//    private List<ContactData> mContactData;
    public LoadContactListPresenter(Activity activity, LoadContactDataListener listener) {
        mActivity = activity;
        mLoadContactDataListener = listener;
//        mContactData = list;
    }

    public interface LoadContactDataListener {
        void requestContactData(Cursor cursor);
    }

    public void onStart() {
        mActivity.getLoaderManager().restartLoader(0, null, loaderCallbacks);
    }

    public void onDestroy() {
        mActivity.getLoaderManager().destroyLoader(0);
    }

    private final LoaderCallbacks<Cursor> loaderCallbacks = new LoaderCallbacks<Cursor>() {

        @Override
        public void onLoaderReset(Loader<Cursor> arg0) {

        }

        @Override
        public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
            if (null != mLoadContactDataListener) {
                mLoadContactDataListener.requestContactData(cursor);
            }
        }

        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {
            CursorLoader cursorLoader = new CursorLoader(mActivity);
            Uri uri = ContactsContract.Contacts.CONTENT_URI;
            final Uri.Builder builder = uri.buildUpon();
            uri = builder.build();
            cursorLoader.setUri(uri);
            cursorLoader.setProjection(_PROJECTION);
            cursorLoader.setSortOrder(ContactsContract.Contacts.SORT_KEY_PRIMARY);
            return cursorLoader;
        }
    };

    public static final String[] _PROJECTION = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
//            ContactsContract.Contacts.SORT_KEY_PRIMARY,
            ContactsContract.Contacts.LOOKUP_KEY,
            ContactsContract.Contacts.PHOTO_ID,
//            ContactsContract.Contacts.STARRED,
//            ContactsContract.Contacts.PINNED,
//            ContactsContract.Contacts.PHOTO_URI,
//            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI
    };
    public static final int ID_COLUMN = 0;
    public static final int NAME_COLUMN = 1;
    public static final int LOOKUP_KEY_COLUMN = 2;
    public static final int PHOTO_ID_COLUMN = 3;
//    public static final String[] CONTACTS_SUMMARY_PROJECTION = new String[]{
//            Data._ID, // 0
//            Email.TYPE, // 1
//            Email.LABEL, // 2
//            Email.DATA, // 3
//            Data.PHOTO_ID, // 4
//            Data.DISPLAY_NAME_PRIMARY, // 5
//            Data.MIMETYPE, // 6
//            RawContacts.ACCOUNT_NAME, // 7
//            Data.PHOTO_THUMBNAIL_URI,// 8
//            Data.LOOKUP_KEY, // 9
//            Data.RAW_CONTACT_ID,//10
//            ContactsContract.CommonDataKinds.Photo.PHOTO,//11
//            ContactsContract.Contacts.LOOKUP_KEY,//12
//            Data.RAW_CONTACT_ID//13
//    };
//    public static final int COLUMN_PHOTO_ID = 4;
//    public static final int COLUMN_PHOTO_URI = 8;
//    public static final int COLUMN_LOOKUP_KEY = 9;
//    public static final int ID_COLUMN_ID = 0;
//    public static final int NAME_COLUMN_ID = 5;
//    public static final int NUMBER_COLUMN_ID = 3;
//    public static final int RAW_CONTACT_ID = 10;
//    public static final int MIMETYPE_COLUMN = 6;
//    public static final int PHOTO_COLUMN = 11;
//    public static final int LOOKUP_KEY_COLUMN = 12;
//    public static final int RAW_CONTACT_ID_COLUMN = 13;
}
